package co.fitstart.market.web.controller.participation.rest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;

import co.fitstart.market.web.interceptor.Router;
import co.fitstart.market.web.interceptor.WechatAuthority;

/**
 * 
 * @author mohanlan
 *
 */
@RestController
@RequestMapping("/participation")
public class ParticipationRestController {
    
//    @Autowired
//    private CoursewareService coursewareService;
    
    private static final Logger logger = LoggerFactory.getLogger(ParticipationRestController.class);
    
   
    /**
     * 
     * @param openid
     * @param courseId
     * @param recommenderOpenid
     * @return
     */
    @RequestMapping(value = "/participate", method = RequestMethod.POST)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    public @ResponseBody String participate(@CookieValue(name = "fitstart_test_openid", required = true) String openid,
                                            @RequestParam("course") Long courseId,
                                            @RequestParam(name = "recommender", required = false) String recommenderOpenid) {
        
        Result result = new Result();
        
        try {
            
//            MarketCourse course = coursewareService.loadCourse(courseId);
            
//            coursewareService.particate(openid, course, recommenderOpenid);
            
            result.success().message("报名成功 ");
            
        } catch (Exception e) {
            
            logger.error("participate - ", e);
            result.fail().message("报名失败");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
}
